package com.pms.exceptions;



/** Base General Purpose Exception Class
*
* <p>
* StudioException is the base-class for all exceptions thrown by the Studio
* System.
*
* <p>
* <i>Important Note</i><br>
* StudioException is declared as a subclass of RuntimeException which
* means that methods throwing a StudioException do not need to declare
* this in their signature.
*
* <p>
* This is a convenience for developers but care must be taken to ensure
* that 'normal' runtime exceptions are caught and correctly handled.
*
* <p>
* TODO: PMC: Make a StudioRuntimeException and force 'normal' StudioExceptions
* to be declared and properly handled?
*
*/
public class StudioException extends RuntimeException {

    //////////////////////////////////////////////////////////////////////
    //
    // Instance Variables
    //

    /** Error number */
    private ErrorCode mErrorCode = ErrorCode.UNSPECIFIED;

    /** Exception parameters */
    private Object[] mErrorParameters=null;

    //////////////////////////////////////////////////////////////////////
    //
    // Public Methods
    //

    /** Create a new BobeException
     *
     * @param aErrorCode      ErrorCode enumeration value
     *
     */
    public StudioException(ErrorCode aErrorCode) {
        this(aErrorCode,aErrorCode.getShortName());
    }

    public StudioException(String aMessage) {
    	super(aMessage);
    }

    
    /** Create a new BobeException with a non-localized description message
     *
     * @param aErrorCode      ErrorCode enumeration value
     * @param aMessage          Description message
     *
     */
    public StudioException(ErrorCode aErrorCode, String aMessage) {
        super(aMessage);
        if(aErrorCode!=null)
        mErrorCode = aErrorCode;

        // Save the message as a parameter
        mErrorParameters=new Object[] { aMessage };
    }

    /** Create a new BobeException with a list of parameters
     *
     * @param aErrorCode      ErrorCode enumeration value
     * @param aErrorParameters  Array of error parameter objects
     *
     */
    public StudioException(ErrorCode aErrorCode, Object[] aErrorParameters) {
        if(aErrorCode!=null)
        mErrorCode = aErrorCode;

        // Save the message as a parameter
        mErrorParameters=aErrorParameters;
    }

    /** Shorthand constructor that accepts 1 parameter for parameterised errors.
     *
     * @param ErrorCode      an ErrorCode enumeration value
     * @param Object         a Parameter for the message
     *
     */
    public StudioException(ErrorCode aErrorCode, Object aParameter) {
        if(aErrorCode!=null)
        mErrorCode = aErrorCode;
        mErrorParameters=new Object[] { aParameter };
    }

    /** Shorthand constructor that accepts 2 parameters for parameterised errors.
     *
     * @param ErrorCode      an ErrorCode enumeration value
     * @param Object         first Parameter for the message
     * @param Object         second Parameter for the message
     *
     */
    public StudioException( ErrorCode aErrorCode, Object aParameter1,
                          Object aParameter2) {
        if(aErrorCode!=null)
        mErrorCode = aErrorCode;
        mErrorParameters=new Object[] { aParameter1, aParameter2 };
    }

    /** Shorthand constructor that accepts 3 parameters for parameterised errors.
     *
     * @param ErrorCode      an ErrorCode enumeration value
     * @param Object         first Parameter for the message
     * @param Object         second Parameter for the message
     * @param Object         third Parameter for the message
     *
     */
    public StudioException( ErrorCode aErrorCode, Object aParameter1,
                          Object aParameter2, Object aParameter3) {
        if(aErrorCode!=null)
        mErrorCode = aErrorCode;
        mErrorParameters=new Object[] { aParameter1, aParameter2, aParameter3 };
    }

    /** Get the error number */
    public ErrorCode getErrorCode() {
        return mErrorCode;
    }

    /** Return the string representation */
    public String toString() {
        StringBuffer buffer=new StringBuffer(mErrorCode.getShortName());
        if (mErrorParameters!=null) {
            buffer.append(" (");
            for (int i=0;i<mErrorParameters.length;i++) {
                if (i>0) {
                    buffer.append(',');
                }
                buffer.append(mErrorParameters[i]);
            }
            buffer.append(")");
        }

        return buffer.toString();
    }
}